/**************************************************************
Dimitrije Ruzic
March 2018

This program creates a shell for bootstrapping standard errors
***************************************************************/
clear programs
clear all
set more off
set matsize 11000

local data "/FILE PATH GOES HERE/"
local boots "/FILE PATH GOES HERE/"
local output "/FILE PATH GOES HERE/"

/*Path to amoeba*/
sysdir set PERSONAL "/FILE PATH GOES HERE/"

/*******************************************************************************
Section 1:
	- creates the dataset for analysis and saves it to the bootstrap folder
*******************************************************************************/
local S1 = 1

/************
Section 1:
*************/
if `S1'==1 {
!gunzip /FILE PATH GOES HERE/Manufacturing_1977_2009.dta.gz
use "`data'Manufacturing_1977_2009.dta", clear
*!gzip /FILE PATH GOES HERE/Manufacturing_1977_2009.dta

/*Keep only the relevant variables*/
keep year et wt fk K L M va te tvs PY2 piship lbdnum

drop if year>2007

gen period = 1 if year<=1982
	replace period = 2 if year>1982 & year<=1987
	replace period = 3 if year>1987 & year<=1992
	replace period = 4 if year>1992 & year<=1997
	replace period = 5 if year>1997 & year<=2002
	replace period = 6 if year>2002 & year<=2007
	
/*Keep ASM Sample Only*/
keep if et==0
drop if wt<0

/*Merge in labor cost shares and elasticities*/
merge m:1 fk period using "`data'PF_Labor_Elasticity_BLS_5y.dta"
drop if _m==2
drop _m

sort fk year
egen lbd = group(lbdnum)
tsset lbd year

/*Production-Function Estimation Variables*/
gen k = log(K)
gen l = log(L)
gen m = log(M)

gen temp = PY2*wt
bys year fk_naics02: egen PiYi_ASM = sum(temp)
drop temp

gen Y_rev = va/piship
gen y_rev = log(Y_rev)
gen y_rev_net = y_rev-L_elasticity_BLS_5y*l

/*Generate Aggregate Output*/
bys fk year: egen Y_ind = sum(Y_rev)
gen q = log(Y_ind)

sort lbd year

/*Generate estimation-pair identifier*/
sort period fk
egen estimation_pair = group(period fk)

/*For each estimation pair, create:
	gid: ordinal unit identifier within estimation_pair
	tt: total unit count within estimation_pair
	count: total observation count within gid
	smalln: ordinal identifier of observations within gid
*/
sort estimation_pair lbd year
by estimation_pair: gen gid = sum(lbd != lbd[_n-1])
by estimation_pair: egen tt = max(gid)
sort estimation_pair gid year
by estimation_pair gid: gen count = _N
by estimation_pair gid: gen smalln = _n

sort estimation_pair gid smalln
order period year lbdnum fk estimation_pair gid tt count smalln

save "`boots'estimation_dataset.dta", replace

keep estimation_pair fk period
duplicates drop estimation_pair fk period, force

save "`boots'estimation_pairs.dta", replace

}
